package com.example.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.news.mapper.ArticleMapper;
import com.example.news.mapper.UserMapper;
import com.example.news.pojo.entity.Article;
import com.example.news.pojo.entity.Comment;
import com.example.news.pojo.entity.User;
import com.example.news.pojo.form.BasePageQuery;
import com.example.news.pojo.form.CommentForm;
import com.example.news.pojo.form.UpdateCommentForm;
import com.example.news.pojo.vo.*;
import com.example.news.service.CommentService;
import com.example.news.mapper.CommentMapper;
import com.example.news.utils.JwtHelper;
import com.example.news.utils.result.PageResult;
import com.example.news.utils.result.Result;
import com.example.news.utils.result.ResultCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 86147
* @description 针对表【comment】的数据库操作Service实现
* @createDate 2023-10-19 14:35:30
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<MyCommentVo> getMyComment(String token, BasePageQuery basePageQuery){
        Long id = jwtHelper.getUserId(token);
        User user = userMapper.selectById(id);

        if(user != null){
            Page<Comment> page = new Page<>(basePageQuery.getPageNum(),basePageQuery.getPageSize());
            IPage<MyCommentVo> iPage = commentMapper.getMyCommentPage(page,id);
            return PageResult.success(iPage);
        }
        return PageResult.build(null,ResultCodeEnum.USER_ERROR);
    }

    @Override
    public PageResult<FirstCommentVo> getFirstCommentByArticleId(Integer id, BasePageQuery basePageQuery){
        Article article =articleMapper.selectById(id);
        if(article != null){
            Page<Comment> page = new Page<>(basePageQuery.getPageNum(), basePageQuery.getPageSize());
            IPage<FirstCommentVo> iPage = commentMapper.getFirstComment(page,id);
            return PageResult.success(iPage);
        }
        return PageResult.build(null, ResultCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public PageResult<SecondCommentVo> getSecondCommentByFirstId(Integer id, BasePageQuery basePageQuery){
        Comment comment = commentMapper.selectById(id);
        if(comment != null){
            Page<Comment> page = new Page<>(basePageQuery.getPageNum(), basePageQuery.getPageSize());
            IPage<SecondCommentVo> iPage = commentMapper.getSecondComment(page,id);
            System.out.println("++++++++++++"+iPage.getRecords());
            return PageResult.success(iPage);
        }
        return PageResult.build(null, ResultCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public Result<ArticleCommentVo> getArticleCommentByArticleId(Integer articleId){
        ArticleCommentVo articleCommentVo = commentMapper.getArticleCommentByArticleId(articleId);
        if(articleCommentVo !=null){
            LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Comment::getArticleId,articleId);
            Integer num = Math.toIntExact(commentMapper.selectCount(queryWrapper));
            articleCommentVo.setCommentNum(num);
            return Result.ok(articleCommentVo);
        }
        return Result.ok(null);
    }

    @Override
    public Result<String> addComment(CommentForm commentForm){
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentForm,comment);
        if(this.save(comment)){
            return Result.ok(null,"发布成功！");
        }
        return Result.build(null, ResultCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public Result<String> deleteComment(Integer id){
        Comment comment = commentMapper.selectById(id);
        if(comment != null){
            if(this.removeById(comment)){
                return Result.ok(null,"删除成功！");
            }
            return Result.build(null, ResultCodeEnum.SYSTEM_ERROR);
        }

        return Result.build(null,500,"评论不存在！");
    }

    @Override
    public Result<String> updateComment(UpdateCommentForm updateCommentForm){
        Comment comment = commentMapper.selectById(updateCommentForm.getId());
        if(comment != null){
            BeanUtils.copyProperties(updateCommentForm,comment);
            if(this.updateById(comment)){
                return Result.ok(null,"修改成功！");
            }
            return Result.build(null, ResultCodeEnum.SYSTEM_ERROR);
        }

        return Result.build(null,500,"评论不存在！");
    }

    @Override
    public PageResult<AllCommentVo> getALlComment(BasePageQuery basePageQuery,String token,String key){
        Long id = jwtHelper.getUserId(token);
        User user = userMapper.selectById(id);

        if(user != null){
            Page<Comment> page = new Page<>(basePageQuery.getPageNum(),basePageQuery.getPageSize());
            IPage<AllCommentVo> iPage = commentMapper.getAllComment(page,key);
            return PageResult.success(iPage);
        }

        return PageResult.build(null,ResultCodeEnum.USER_ERROR);
    }

}




